﻿using NaireSystem.Api;
using NaireSystem.ViewModels;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using TYControls;
using NaireSystem.Datas;
using NaireSystem.DBEntity;
using NaireSystem.Enums;

namespace NaireSystem.Views
{
    /// <summary>
    /// CtrlNaireQts.xaml 的交互逻辑
    /// </summary>
    public partial class CtrlNaireQts : TYUserControl
    {
        ViewModelNaireQts vm = new ViewModelNaireQts();
        public CtrlNaireQts()
        {
            InitializeComponent();
            vm = new ViewModelNaireQts();
            this.DataContext = vm;
            this.Loaded += CtrlNaireQts_Loaded;
        }

        private async void CtrlNaireQts_Loaded(object sender, RoutedEventArgs e)
        {
            if (!await LoadUser())
            {
                MessageBox.Show("获取用户失败");
                return;
            }
            if (!await InitDictData())
            {
                return;
            }
            BtnClick_Search(null, null);
        }

        private async Task<bool> LoadUser()
        {
            vm.ListUser.Clear();
            var rData = await Run(() =>
            {
                return ServerApi.GetUserList();
            });
            if (rData.code != 0)
            {
                this.ShowMsg(rData.msg);
                return false;
            }
            rData.data.ForEach(x =>
            {
                vm.ListUser.Add(new StringStringData { Key = x.s_ic_no, Value = x.s_name });
            });
            return true;
        }

        private async void BtnClick_Search(object sender, RoutedEventArgs e)
        {
            int pageIndex = pager.CurrentPage;
            //int pageSize=pager.MaxPage
            QtQueryDto queryDto = new QtQueryDto()
            {
                createCode = vm.SelectedUser?.Key,
                tag = vm.SelectTag?.Value,
                dtEnd = vm.DtEnd,
                dtStart = vm.DtStart,
                title = vm.Title,
                examType = (int)EnumExamType.Naire,
            };
            vm.ListQt.Clear();

            var rData = await Run(() =>
            {
                return QtApi.GetListQt(new PageInfo(pageIndex, 30), queryDto);

            });
            if (rData.code != 0)
            {
                this.ShowMsg(rData.msg);
                return;
            }
            pager.MaxPage = rData.page.PageCount();
            rData.data.ForEach(x =>
            {
                QtModel quetionModel = UIDataHelper.DataAsUI(x);
                quetionModel.Index = (pageIndex - 1) * rData.page.PageSize + vm.ListQt.Count + 1;

                //var listTag = x.listTag;
                //if (listTag != null)
                //{
                //    quetionModel.StrTag = string.Join(",", listTag);
                //}
                vm.ListQt.Add(quetionModel);
            });

        }

        private void BtnClick_EditItem(object sender, RoutedEventArgs e)
        {
            var dx = (sender as Button).DataContext as QtModel;
            ViewNaireEditQt view = new ViewNaireEditQt(dx.data);
            view.ShowDialog();
            BtnClick_Search(null, null);
        }

        private async Task<bool> InitDictData()
        {
            var ret = await Run(() =>
            {
                return DictDataApi.GetList(new Tbl_DictData
                {
                    dict_type = $"{EnumDictType.naire_tag_type}"
                });
            });
            if (ret.code != 0)
            {
                this.ShowMsg(ret.msg);
                return false;
            }
            vm.ListTag.Clear();
            foreach (var item in ret.data)
            {
                IntStringData model = new IntStringData { Key = item.dict_value, Value = item.dict_label };
                vm.ListTag.Add(model);
            }
            return true;
        }

        private async void BtnClick_DeleteItem(object sender, RoutedEventArgs e)
        {
            var model = (sender as Button).DataContext as QtModel;
            if (!this.ShowYesNo($"是否确认删除 ：{model.Title}"))
            {
                return;
            }
            var ret = await Run(() =>
            {
                return QtApi.DeleteQt(model.data.qtId);
            });

            if (ret.code != 0)
            {
                this.ShowMsg(ret.msg);
                return;
            }

            BtnClick_Search(null, null);
        }

        private void pager_CurrentPageChanged(object sender, Panuon.WPF.SelectedValueChangedRoutedEventArgs<int> e)
        {
            BtnClick_Search(null, null);
        }

        private void BtnClick_Add(object sender, RoutedEventArgs e)
        {
            //var dx = (sender as Button).DataContext as QtModel;
            ViewNaireEditQt view = new ViewNaireEditQt(null);
            view.ShowDialog();
            BtnClick_Search(null, null);
        }
    }
}
